/**
 * 
 */
package net.mikoo.seals.boss.biz.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.boss.biz.mapper.client.GeoMapper;
import net.mikoo.seals.boss.biz.services.GeoServices;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.common.helper.GeoHelper;
import net.mikoo.seals.boss.model.Geo;
import net.mikoo.seals.boss.model.GeoAssoc;
import net.mikoo.seals.boss.model.GeoType;
import net.sf.json.JSONArray;

/**
 * 
 * 
 * @Filename GeoServicesImpl.java
 *
 * @Description
 *
 * @Version 1.0
 *
 * @Author huangxiaoming
 *
 * @Email huangxm129@163.com
 * 
 * @History
 *          <li>Author: huangxiaoming</li>
 *          <li>Date: 2014骞�12鏈�4鏃�</li>
 *          <li>Version: 1.0</li>
 *          <li>Content: create</li>
 *
 */
@Service
public class GeoServicesImpl extends BaseServices implements GeoServices {

	Logger logger = Logger.getLogger(GeoServicesImpl.class);

	@Autowired
	private GeoMapper geoMapper;

	@Override
	public void save(final Geo geo) {

		 clientTransactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {

				try {

					if (geo == null) {
						throw new ParametersException("geo is null!");
					}

					if (StringUtils.isBlank(geo.getGeoName())) {
						throw new ParametersException("getGeoName is blank!");
					}
					if (geo.getSequence() == null) {
						geo.setSequence(0);
					}

					geoMapper.insert(geo);
					GeoAssoc geoAssoc = new GeoAssoc();
					geoAssoc.setGeoId(geo.getGeoId());
					geoAssoc.setGeoIdTo(geo.getParentId() == null ? 0L : geo.getParentId());
					geoMapper.insertAssoc(geoAssoc);
				} catch (ParametersException e) {
					status.setRollbackOnly();
					throw e;

				} catch (Exception e) {
					status.setRollbackOnly();
					throw e;
				}

			}
		});

	}

	@Override
	public void modifyByPrimaryKey(Geo geo) {

		clientTransactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {

				try {

					if (geo == null) {
						throw new ParametersException("geo is null!");
					}
					geoMapper.updateByPrimaryKey(geo);
					geoMapper.deletetAssoc(geo.getGeoId());
					GeoAssoc geoAssoc = new GeoAssoc();
					geoAssoc.setGeoId(geo.getGeoId());
					geoAssoc.setGeoIdTo(geo.getParentId() == null ? 0L : geo.getParentId());
					geoMapper.insertAssoc(geoAssoc);

				} catch (ParametersException e) {
					status.setRollbackOnly();
					throw e;

				} catch (Exception e) {
					status.setRollbackOnly();
					throw e;
				}

			}
		});

	}

	@Override
	public Geo loadByPrimaryKey(Long geoId) {
		
			if(geoId == null){
				throw new ParametersException("geoId is null!");
			}
			
			Geo geo = geoMapper.selectByPrimaryKey(geoId);
			return geo;
	}

	@Override
	public void deleteByPrimaryKey(Long geoId) {

	}

	@Override
	public List<Geo> loadAsTree() {

		List<Geo> geos = new ArrayList<Geo>();
		List<Geo> childs = geoMapper.selectAll();
		geos = GeoHelper.convertToTree(childs, 0L, 0);

		return geos;

	}

	@Override
	public List<Geo> loadByParentId(Long parentId) {

		if (parentId == null) {
			parentId = 0L;
		}
		List<Geo> geos = geoMapper.selectByParentId(parentId);
		return geos;
	}

	@Override
	public List<GeoType> loadTypes() {

		List<GeoType> geoTypes = geoMapper.selectTypes();

		return geoTypes;
	}

	@Override
	public JSONArray refreshCache() {

		List<Geo> datas = geoMapper.selectAll();
		JSONArray jsonArray = GeoHelper.convertToJsonTreet1(datas, 100000L);
		// System.out.println(jsonArray.toString());

		return jsonArray;
	}

}